/// <summary>
// CodeFx - A .NET framework
// Laurent Morisseau
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Laurent Morisseau
/// </summary>
/// <author><a href="mailto:laurent.morisseau@gmail.com">Laurent Morisseau</a></author>

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Collections;
using System.Web;
using System.Web.SessionState;

namespace CodeFx.Web
{
    public enum EPersistanceType
    {
        Session,
        Cache
    }

    public struct PersistanceViewerDAL
    {
        public object obj;
        private string key;
        public EPersistanceType type;

        public string Objet
        {
            get
            {
                return obj.ToString();
            }
        }
        public string Key
        {
            get
            {
                return key;
            }
            set
            {
                key = value;
            }
        }
        public string Type
        {
            get
            {
                return type.ToString();
            }
        }
    }

    [DataObject]
    public static class PersistanceViewerDTO
    {
        [DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static ArrayList Select()
        {
            ArrayList Persistances = new ArrayList();
            /// Cache
            IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                if (!CacheEnum.Key.ToString().Contains("System."))
                {
                    PersistanceViewerDAL persistance = new PersistanceViewerDAL();
                    persistance.Key = CacheEnum.Key.ToString();
                    if (CacheEnum.Value.GetType().Equals(typeof(System.String[])))
                    {
                        string[] values = CacheEnum.Value as string[];
                        StringBuilder valuebuilder = new StringBuilder();
                        foreach (string value in values)
                        {
                            valuebuilder.Append("[");
                            valuebuilder.Append(value);
                            valuebuilder.Append("]");
                            persistance.obj = valuebuilder.ToString();
                        }
                    }
                    else
                        persistance.obj = CacheEnum.Value;
                    persistance.type = EPersistanceType.Cache;
                    Persistances.Add(persistance);
                }
            }
            /// Session
            IEnumerator SessionKeys = HttpContext.Current.Session.Keys.GetEnumerator();
            while (SessionKeys.MoveNext())
            {
                PersistanceViewerDAL persistance = new PersistanceViewerDAL();
                persistance.Key = SessionKeys.Current.ToString();
                if (HttpContext.Current.Session[SessionKeys.Current.ToString()].GetType().Equals(typeof(System.String[])))
                {
                    string[] values = HttpContext.Current.Session[SessionKeys.Current.ToString()] as string[];
                    StringBuilder valuebuilder = new StringBuilder();
                    foreach (string value in values)
                    {
                        valuebuilder.Append("[");
                        valuebuilder.Append(value);
                        valuebuilder.Append("]");
                        persistance.obj = valuebuilder.ToString();
                    }
                }
                else
                    persistance.obj = HttpContext.Current.Session[SessionKeys.Current.ToString()];
                persistance.type = EPersistanceType.Session;
                Persistances.Add(persistance);
            }
            return Persistances;
        }

        [DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public static void Delete(PersistanceViewerDAL persistance)
        {
            switch (persistance.type)
            {
                case EPersistanceType.Cache:
                    HttpContext.Current.Cache.Remove(persistance.Key);
                    break;
                case EPersistanceType.Session:
                    HttpContext.Current.Session.Remove(persistance.Key);
                    break;
            }
        }

        [DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static ArrayList ContextError()
        {
            ArrayList Errors = new ArrayList();
            ArrayList Persistances = Select();
            Persistances.Sort(new myReverserClass());
            foreach (PersistanceViewerDAL persistance in Persistances)
            {
                switch (persistance.type)
                {
                    case EPersistanceType.Cache:
                        {
                            break;
                        }
                    case EPersistanceType.Session:
                        {
                            break;
                        }
                }
            }
            return Errors;
        }

        private class myReverserClass : IComparer
        {
            int IComparer.Compare(object x, object y)
            {
                PersistanceViewerDAL xx = (PersistanceViewerDAL)x;
                PersistanceViewerDAL yy = (PersistanceViewerDAL)y;

                return xx.Key.CompareTo(yy.Key);
            }

        }

    }
}
